<template>
  <div class="project-audit-detail-view">

    <modules-title class="title-position" :title-name="datas.Flow.Name">

      <template slot="btn">
        <el-button plain @click="backClick">返回</el-button>

        <approval style="margin-right: 10px" :affair.sync="datas" :is-text="false" @refresh="refreshData" @submit="onSubmit" @toEdit="toEdit" @cancel="onCancel">
          <template #next v-if="isCost">
            <el-form ref="form" :model="costData" :rules="rules" label-width="97px">
              <el-form-item label="费用总计" prop="project_cost">
                <el-input v-model="costData.project_cost"></el-input>
              </el-form-item>
              <el-form-item label="总金额" prop="project_amount">
                <el-input v-model="costData.project_amount"></el-input>
              </el-form-item>
            </el-form>
          </template>
        </approval>
<!--        <el-button v-if="isAudit && (datas.State != 'Done' && datas.State != 'Refuse')" type="primary" size="mini" @click="auditClick">{{itemName}}</el-button>-->
      </template>

    </modules-title>

    <div v-if="datas.Flow" class="pj-content">

      <!-- 项目方案书审核  -->
      <div v-if="datas.Flow.Code == 'ProjectFirstCheck'">

        <item-title title="基本信息" />

        <information-view no-operation :datas="projectData" />

        <meeting-list-view :project="projectData" business="SchemeReview"></meeting-list-view>

      </div>

      <!-- 项目计划审核  -->
      <div v-if="datas.Flow.Code == 'ProjectPlanCheck'">

        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="基本信息" name="informationView" />
          <el-tab-pane label="项目成员" name="members" />
          <el-tab-pane label="项目任务" name="projectTaskView" />
          <el-tab-pane label="交付物清单" name="documentView" />
        </el-tabs>

        <!--          <information-view is-other :datas="projectData" v-if="activeName == 'information'"></information-view>-->

        <!--          <members :datas="projectData" v-if="activeName == 'member'"></members>-->

        <component :is="activeName" ref="component" is-other :datas="projectData" />

      </div>

      <!-- 项目暂停终止审核  -->
      <div v-else-if="datas.Flow.Code == 'ProjectPause' || datas.Flow.Code == 'ProjectStop'">

        <item-title title="基本信息" />
        <stop-or-pause-view :datas="stopData" />

      </div>

      <!-- 项目信息变更审核  -->
      <div v-else-if="datas.Flow.Code == 'ChangeProjectInfo'">

        <item-title title="基本信息" />

        <project-base-change-view :datas="changeData" />

      </div>

      <!-- 项目成员变更审核  -->
      <div v-else-if="datas.Flow.Code == 'ChangeProjectMember'">

        <item-title title="基本信息" />

        <member-change-view :datas="changeData" :projects="projects"/>

      </div>

      <!-- 项目任务信息变更  -->
      <div v-else-if="datas.Flow.Code == 'ChangeProjectTask'">

        <item-title title="基本信息" />

        <task-change-view :datas="changeData" />

      </div>

      <!-- 任务批量变更  -->
      <div v-else-if="datas.Flow.Code == 'ChangeProjectMulTask'">

        <item-title title="基本信息" />

        <project-task-change-view :datas="changeData" :projects="projects"/>

      </div>

      <!-- 任务验收  -->
      <div v-else-if="datas.Flow.Code == 'TaskCheck'">

        <item-title title="基本信息" />

        <task-detail-view :datas="taskData" :project="projects" :is-disable="!isAudit" />

        <meeting-list-view :task="taskData"></meeting-list-view>

      </div>

      <!-- 项目报告验收  -->
      <div v-else-if="datas.Flow.Code == 'ProjectEndCheck'">
        <item-title title="基本信息" />
        <project-check-view :datas="projectData" />
      </div>

      <!-- 项目报告验收  -->
      <div v-else-if="datas.Flow.Code == 'ProjectEnd2Check'">
        <item-title title="基本信息" />
        <project-end :datas="projectData" />

        <meeting-list-view :project="projectData" business="ProjectAcceptance"></meeting-list-view>
      </div>

      <comment-view :datas="datas" :is-show="true" />

    </div>

    <el-dialog
      title="审核"
      v-if="dialogVisible"
      :visible.sync="dialogVisible"
      width="800px"
      :before-close="handleClose"
    >

      <el-form :model="formData" class="demo-form-inline" label-width="90px" size="medium" label-position="left">
        <el-form-item label="审批结论">
          <el-radio-group v-model="formData.radio">
            <el-radio v-for="(item,index) in bottons" :key="index" :label="item.Type">{{ item.Name }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见">
          <el-input
            v-model="formData.textarea"
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import documentView from "./modules/documentView";
  import meetingListView from "./modules/meetingListView";
  import projectEnd from "./modules/projectEnd";
  import {undoX} from "../../../../api/stability/change";
  import approval from "../../../../components/approval/approval";
    import projectCheckView from './modules/projectCheckView'
    import taskView from './modules/taskView'
    import taskDetailView from './modules/taskDetailView'
    import projectTaskChangeView from './modules/projectTaskChangeView'
    import taskChangeView from './modules/taskChangeView'
    import memberChangeView from './modules/memberChangeView'
    import projectBaseChangeView from './modules/projectBaseChangeView'
    import stopOrPauseView from './modules/stopOrPauseView'
    import projectTaskView from './modules/projectTaskView'
    import members from './modules/members'
    import informationView from './modules/informationView'
    export default {
        name: 'Index',
        components: {
            informationView,
            members,
            projectTaskView,
            stopOrPauseView,
            projectBaseChangeView,
            memberChangeView,
            taskChangeView,
            projectTaskChangeView,
            taskDetailView,
            taskView,
            projectCheckView,
            approval,
            projectEnd,
            meetingListView,
            documentView
        },
        data: function() {
            return {
                activeName: 'informationView',
                isAudit: false,
                s_type: 0, // 1:项目  2:变更   3:项目暂停、停止   4:任务
                title: '详情',
                itemName:'审核',
                user_id: '',
                bottons: [],
                dialogVisible: false,
                formData: {
                    radio: 'Next',
                    textarea: '',
                    files:[],
                },
                datas: {},
                projectData: {},
                stopData: {},
                changeData: {},
                taskData: {},
                comments: [],
                projects:{},
                costData:{
                    project_cost:'',
                    project_amount:''
                },
                isCost:false,

                rules: {
                    project_amount: [
                        { required: true, message: '请输入总金额', trigger: 'blur' }
                    ],
                    project_cost: [
                        { required: true, message: '请输入费用总计', trigger: 'blur' }
                    ],
                },

            }
        },
        created: function() {
            this.user_id = this.$store.getters.userInfo.user.id
        },
        mounted: function() {
            if (this.$route.query.id) {
                console.log(this.$route.query.id)
                this.getData(this.$route.query.id)
            }
        },
        methods: {
            refreshData:function(){
                this.getData(this.$route.query.id)
            },
            handleClick: function() {

            },
            getData: function(id) {
                this.$api.PROJECT_START_CHECK_DETAIL(id).then(res => {
                    if (res.code == 200) {
                        this.datas = res.data

                        this.isCost = false
                        if (res.data.Next && res.data.Next.length){
                            if (res.data.Next[0].EditFields){
                                if (res.data.Next[0].EditFields.indexOf('project_cost')!=-1 || res.data.Next[0].EditFields.indexOf('project_amount')!=-1){
                                    this.isCost = true
                                }
                            }
                        }

                        this.getButton()
                        this.getCodeStr()
                        this.getOtherData()
                    }
                })
            },
            getOtherData: function() {
                this.projects = this.datas.Template.FieldValues.data.project
                if (this.s_type == 1) {
                    this.$api.PROJECT_START_CHECK_PROJECT_DETAIL(this.datas.Id,this.datas.Flow.Code).then(res => {
                        if (res.code == 200) {
                            //this.projectData = res.data ? res.data : {}
                            if (res.data && res.data.reality_end_time){
                                this.projectData.reality_end_time = res.data.reality_end_time
                            }
                        } else {
                            this.$message.error(res.msg)
                        }
                    })
                    this.projectData = this.datas.Template.FieldValues.data

                } else if (this.s_type == 3) {
                    this.stopData = this.datas.Template.FieldValues.data
                    this.$api.PROJECT_START_CHECK_STOP_DETAIL(this.datas.Id).then(res => {
                        if (res.code == 200) {
                            // this.stopData = res.data ? res.data : {}
                            this.stopData.id = res.data.id
                        } else {
                            this.$message.error(res.msg)
                        }
                    })

                } else if (this.s_type == 2) {
                    this.changeData = this.datas.Template.FieldValues.data
                    this.$api.PROJECT_START_CHECK_CHANGE_DETAIL(this.datas.Id).then(res => {
                        if (res.code == 200) {
                            // this.changeData = res.data ? res.data : {}
                            // if (res.data.project_id){
                            //     this.getProject(res.data.project_id)
                            // }
                            this.changeData.id = res.data.id
                        } else {
                            this.$message.error(res.msg)
                        }
                    })

                } else if (this.s_type == 4) {
                    this.taskData = this.datas.Template.FieldValues.data
                    this.$api.PROJECT_START_CHECK_TASK_DETAIL(this.datas.Id).then(res => {
                        if (res.code == 200) {
                            // this.taskData = res.data ? res.data : {}
                            // if (res.data.project_id){
                            //     this.getProject(res.data.project_id)
                            // }
                            this.taskData.id = res.data.id
                            this.taskData.real_end_date = res.data.real_end_date
                        } else {
                            this.$message.error(res.msg)
                        }
                    })

                }
            },
            getProject:function(id){
                this.$api.PROJECT_MANAGER_PROJECT_DETAIL(id).then(res=>{
                    if (res.code == 200){
                        this.projects = res.data;

                    }
                }).catch(err=>{

                })
            },
            getButton: function() {
                if (this.datas.State != 'Done' || this.datas.State != 'Refuse') {
                    if (this.datas.Next && this.datas.Next.length) {
                        for (let i = 0; i < this.datas.Next.length; i++) {
                            const data = this.datas.Next[i]
                            const index = data.Staffs.findIndex(a => a == this.user_id)
                            if (index != -1) {
                                this.isAudit = true
                                this.itemName = data.ItemName
                                this.bottons = data.Buttons
                                const inx = this.bottons.findIndex(a => a.Type == 'Cancel')
                                if (inx != -1) {
                                    this.bottons.splice(inx, 1)
                                }
                                const inx2 = this.bottons.findIndex(a => a.Type == 'Reject')
                                if (inx2 != -1) {
                                    this.bottons.splice(inx2, 1)
                                }
                                break
                            }
                        }
                    }
                }
            },
            backClick: function() {

                this.$router.back()
            },
            auditClick: function() {
                this.formData  = {
                    radio: 'Next',
                    textarea: '',
                    files:[],
                }
                this.dialogVisible = true
            },
            handleClose: function() {
                this.dialogVisible = false
            },
            toEdit:function(affairs){
                this.$message.warning("暂无此功能")
            },
            //affairsId, stepId  //, type  //选择类型, template //备注, RejectStepId  //步骤id, isVisible
            onCancel:function(affairsId,stepId,type,template,RejectStepId,call){
                let data={
                    flow_id:this.datas.Id,
                    model_type:this.datas.Flow.Code
                }
                undoX(data).then(res=>{
                    if(res.code==200){
                        // call(true)
                        this.$message.success('成功')
                        this.$route.params.refresh = "true";
                        this.$router.back()
                    }else {
                        this.$message.error(res.msg)
                    }
                })
            },
            onSubmit:function(affairsId,stepId,type,template,RejectStepId,call){
                this.formData.radio = type;
                this.formData.textarea = template.Remark;
                this.formData.files = template.Files;
                console.log(template.Files)
                this.confirmClick(call);

            },
            confirmClick: function(call) {

                if (this.isCost && this.$refs.form){
                    this.$refs.form.validate((valid) => {
                        if (!valid){
                            return
                        }
                    })
                }

                let status = 0
                if (this.formData.radio == 'Next') {
                    status = 1
                } else if (this.formData.radio == 'Refuse') {
                    status = 3
                } else if (this.formData.radio == 'Reject') {
                    status = 2
                }
                const data = {
                    status: this.formData.radio,
                    opinion: this.formData.textarea,
                    files:this.formData.files
                }
                if (this.s_type == 1) {
                    data.id = this.projectData.id
                    data.project_cost = this.costData.project_cost;
                    data.project_amount = this.costData.project_amount;

                    this.$api.PROJECT_START_PROJECT_CHECK(data,this.datas.Flow.Code).then(res => {
                        if (res.code == 200) {
                            this.$message.success('成功')
                            this.$route.params.refresh = "true";
                            this.$router.back()
                        } else {
                            this.$message.error(res.msg)
                        }
                    }).catch(err => {

                    })
                } else if (this.s_type == 3) {
                    data.id = this.stopData.id
                    this.$api.PROJECT_START_STOP_CHECK(data).then(res => {
                        if (res.code == 200) {
                            this.$message.success('成功')
                            this.$route.params.refresh = "true";
                            this.$router.back()
                        } else {
                            this.$message.error(res.msg)
                        }
                    }).catch(err => {

                    })
                } else if (this.s_type == 2) {
                    data.id = this.changeData.id
                    this.$api.PROJECT_START_CHANGE_CHECK(data).then(res => {
                        if (res.code == 200) {
                            this.$message.success('成功')
                            this.$route.params.refresh = "true";
                            this.$router.back()
                        } else {
                            this.$message.error(res.msg)
                        }
                    }).catch(err => {

                    })
                } else if (this.s_type == 4) {
                    data.id = this.taskData.id
                    this.$api.PROJECT_START_TASK_CHECK(data).then(res => {
                        if (res.code == 200) {
                            this.$message.success('成功')
                            this.$route.params.refresh = "true";
                            this.$router.back()
                        } else {
                            this.$message.error(res.msg)
                        }
                    }).catch(err => {

                    })
                }
            },
            getCodeStr: function() {
                let str = ''
                if (this.datas.Flow) {
                    switch (this.datas.Flow.Code) {
                        case 'ChangeProjectInfo':
                            str = '项目信息变更'
                            this.s_type = 2
                            break
                        case 'ChangeProjectMember':
                            str = '项目成员变更'
                            this.s_type = 2
                            break
                        case 'ChangeProjectTask':
                            str = '项目任务变更'
                            this.s_type = 2
                            break
                        case 'ProjectPause':
                            str = '项目暂停'
                            this.s_type = 3
                            break
                        case 'ProjectStop':
                            str = '项目终止'
                            this.s_type = 3
                            break
                        case 'ProjectFirstCheck':
                            str = '项目方案书'
                            this.s_type = 1
                            break
                        case 'ProjectPlanCheck':
                            str = '项目计划审批'
                            this.s_type = 1
                            break
                        case 'ProjectEndCheck':
                            str = '项目验收材料审批'
                            this.s_type = 1
                            break
                        case 'ProjectEnd2Check':
                            str = '项目报告验收'
                            this.s_type = 1
                            break
                        case 'TaskCheck':
                            str = '任务验收'
                            this.s_type = 4
                            break
                        case 'ChangeProjectMulTask':
                            str = '项目计划变更'
                            this.s_type = 2
                            break
                        default:
                            str = ''
                            break
                    }
                }

                if (str) {
                    this.title = str + this.title
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
  @import "../../../../assets/scss/views/projectManagement/audit/detail.scss";
  .project-audit-detail-view .el-card{
    border: 0px;
  }
  .project-audit-detail-view .el-card .el-card__header{
    border-bottom:0px
  }
</style>
